package com.vm.activity;

import org.json.JSONObject;

import com.android.volley.RequestQueue;
import com.android.volley.VolleyError;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.android.volley.toolbox.ImageLoader.ImageListener;
import com.vm.commons.CurrentUser;
import com.vm.commons.VMConst;
import com.vm.db.LocalUserInfoDao;
import com.vm.model.Goods;
import com.vm.model.LocalUserInfo;
import com.vm.model.UserAddress;
import com.vm.utils.JsonUtils;
import com.wwt.app.R;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 
 * @ProjectName: [WeiMai]
 * @Author: [kent.wang]
 * @CreateDate: [2015年3月18日 上午9:33:18]
 * @Version: [v1.0]
 * 
 */
public class OrderConfirmActivity extends Activity {

	// ImageView iv_select;
	LinearLayout llayout_address;
	ImageView iv_goodsImage;
	ImageView imgbtn_search_back;
	TextView tv_name;
	TextView tv_phone;
	TextView tv_goodsName;
	TextView tv_address;
	TextView tv_goodsPrice;
	TextView tv_integral;
	Button btn_confirm;
	private LocalUserInfoDao luidao;
	UserAddress userAddress;
	RequestQueue mQueue;
	ImageLoader mImageLoader;
	Goods goods;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.order_confirm);

//		init();
//		luidao = new LocalUserInfoDao(this);
//		goods = (Goods) getIntent().getSerializableExtra("goods");
//		
//		tv_goodsName.setText(goods.getName());
//		tv_goodsPrice.setText(Integer.toString(goods.getIntegral()));
//		tv_integral.setText(Integer.toString(goods.getIntegral()));
//
//		mQueue = Volley.newRequestQueue(this);
//		mImageLoader = new ImageLoader(mQueue, new BitmapCache());
//		ImageListener listener = ImageLoader
//				.getImageListener(iv_goodsImage,
//						android.R.drawable.ic_menu_rotate,
//						android.R.drawable.ic_delete);
//
//		mImageLoader.get(VMConst.URL + goods.getImageUrl(), listener);
//
//		String url = VMConst.REQUEST_URL + "getFirstAddress?userId="
//				+ CurrentUser.USERID;
//		JsonObjectRequest addressRequest = new JsonObjectRequest(url, null,
//				new Listener<JSONObject>() {
//
//					@Override
//					public void onResponse(JSONObject arg0) {
//						userAddress = JsonUtils.jsonToBean(arg0,
//								UserAddress.class);
//						tv_name.setText(userAddress.getName());
//						tv_phone.setText(userAddress.getTelephone());
//						tv_address.setText(userAddress.getAddress());
//					}
//				}, new ErrorListener() {
//
//					@Override
//					public void onErrorResponse(VolleyError arg0) {
//						tv_address.setText("请点击创建收货地址");
//					}
//				});
//		mQueue.add(addressRequest);

	}

	/**
	 * 初始化
	 */
	public void init() {
//		tv_name = (TextView) findViewById(R.id.tv_name);
//		tv_phone = (TextView) findViewById(R.id.tv_phone);
//		tv_address = (TextView) findViewById(R.id.tv_address);
//
//		iv_goodsImage = (ImageView) findViewById(R.id.iv_goodsImage);
//		imgbtn_search_back = (ImageView)findViewById(R.id.imgbtn_search_back);
//		tv_goodsName = (TextView) findViewById(R.id.tv_goodsName);
//		tv_goodsPrice = (TextView) findViewById(R.id.tv_goodsPrice);
//		tv_integral = (TextView) findViewById(R.id.tv_integral);
//		btn_confirm = (Button) findViewById(R.id.btn_confirm);
//
//		btn_confirm.setOnClickListener(btn_confirmListener);
//		llayout_address = (LinearLayout) findViewById(R.id.llayout_address);
//		llayout_address.setOnClickListener(address_ClickListener);
//		imgbtn_search_back.setOnClickListener(btn_backListener);
//		// iv_select = (ImageView)findViewById(R.id.iv_select);
//		// iv_select.setOnClickListener(ClickListener);
	}

	OnClickListener btn_confirmListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			if (userAddress != null) {

				String url = VMConst.REQUEST_URL + "android_addOrder?userId="
						+ CurrentUser.USERID+"&goodsId="+goods.getId()+"&addressId="+userAddress.getId();
				StringRequest confirmRequest = new StringRequest(url,
						new Listener<String>() {

							@Override
							public void onResponse(String arg0) {
								if (arg0.equals("1")) {
									// 成功
									int intergral = Integer.parseInt(tv_integral.getText().toString());
									CurrentUser.INTERGRAL = CurrentUser.INTERGRAL - intergral;
									
									luidao.update();
									Intent intent = new Intent(
											OrderConfirmActivity.this,
											OrderSuccessActivity.class);
									startActivity(intent);
								} else {
									// 失败
									Toast toast = Toast.makeText(
											OrderConfirmActivity.this, "订单提交失败",
											Toast.LENGTH_SHORT);
									toast.show();
								}
							}
						}, new ErrorListener() {

							@Override
							public void onErrorResponse(VolleyError arg0) {
								// //提示积分不足
								Toast toast = Toast.makeText(
										OrderConfirmActivity.this, "订单提交失败",
										Toast.LENGTH_SHORT);
								toast.show();
							}
						});
				mQueue.add(confirmRequest);
			}

		}
	};

	OnClickListener address_ClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			Intent intent = new Intent(OrderConfirmActivity.this,
					AddressSelectActivity.class);
//			intent.putExtra("userId", userInfo.getUserId());
			startActivity(intent);
			
			OrderConfirmActivity.this.finish();
		}
	};
	
	/**
	 * imgbtn_search_back 返回按钮监听
	 */
	OnClickListener btn_backListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			onBackPressed();
		}
	};

	/**
	 * 手机自带返回按钮监听
	 */
	@Override
	public void onBackPressed() {
		super.onBackPressed();
		
	}

}
